package com.yztc.xiami_app.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.yztc.xiami_app.R;
import com.yztc.xiami_app.adapter.MyAdapter;
import com.yztc.xiami_app.adapter.mainactivity.Navigation_ViewPager_Adapter;
import com.yztc.xiami_app.helper.NoScrollViewPager;
import com.yztc.xiami_app.ui.fragment.ClassifyFragment;
import com.yztc.xiami_app.ui.fragment.HomePageFragment;
import com.yztc.xiami_app.ui.fragment.MineFragment;
import com.yztc.xiami_app.ui.fragment.ShopCartFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 虾米移动应用平台
 */
public class MainActivity extends FragmentActivity implements AdapterView.OnItemClickListener {

    private String[] strs = { "QQ", "微信", "帖吧", "微博", "邮箱", "云盘", "浏览器",
            "输入法", "任务管理器" };
    private ListView listView;
    private MyAdapter adapter;
    private ClassifyFragment myFragment;
    public static int mPosition;

    private long backStartTime, backEndTime;
    @BindView(R.id.viewPager_main)
    NoScrollViewPager mainVp; //底部导航对应的ViewPager
    @BindView(R.id.rg_main)
    RadioGroup mainRg; //底部导航选择按钮

    //Main ViewPager对应的数据源，也就是底部导航对应的Fragment的集合
    private List<Fragment> mFragmentList = new ArrayList<>();
    private FragmentManager mManager;
    private Navigation_ViewPager_Adapter navigationViewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //设置ViewPager，不能滑动
        mainVp.setNoScroll(true);


        initData();
        initViewpager();
    }




    private void initView() {
        // TODO Auto-generated method stub
        listView = (ListView) findViewById(R.id.listview);

        adapter = new MyAdapter(this, strs);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);

        //创建MyFragment对象
        myFragment = new ClassifyFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, myFragment);
        //通过bundle传值给MyFragment
        Bundle bundle = new Bundle();
        bundle.putString(ClassifyFragment.TAG, strs[mPosition]);
        myFragment.setArguments(bundle);
        fragmentTransaction.commit();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backEndTime = System.currentTimeMillis();
            if (backEndTime - backStartTime > 2000) {
                Toast.makeText(this, "连续按两次退出程序", Toast.LENGTH_SHORT).show();
                backStartTime = backEndTime;
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onResume() {
        super.onResume();

        //底部导航点击事件的处理
        mainRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //底部导航，单选按钮的选择事件的处理，
                switch (checkedId){
                    case R.id.home_page_rb:
                        mainVp.setCurrentItem(0);
                        break;
                    case R.id.classify_rb:
                        mainVp.setCurrentItem(1);
                        break;
                    case R.id.shop_cart_rb:
                        mainVp.setCurrentItem(2);
                        break;
                    case R.id.mine_rb:
                        mainVp.setCurrentItem(3);
                        break;
                }


            }
        });
    }

    //初始化主ViewPager的数据源，也就是底部导航对应的4个Fragment
    private void initData() {
        mFragmentList.add(new HomePageFragment());
        mFragmentList.add(new ClassifyFragment());
        mFragmentList.add(new ShopCartFragment());
        mFragmentList.add(new MineFragment());
    }

    private void initViewpager() {
        mManager = getSupportFragmentManager();
        navigationViewPagerAdapter = new Navigation_ViewPager_Adapter(mManager,mFragmentList);
        //ViewPager加载适配器
        mainVp.setAdapter(navigationViewPagerAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        //拿到当前位置
        mPosition = position;
        //即使刷新adapter
        adapter.notifyDataSetChanged();
        for (int i = 0; i < strs.length; i++) {
            myFragment = new ClassifyFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                    .beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, myFragment);
            Bundle bundle = new Bundle();
            bundle.putString(ClassifyFragment.TAG, strs[position]);
            myFragment.setArguments(bundle);
            fragmentTransaction.commit();

        }
    }
}
